﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using BLL;
using Model;
namespace UI
{
    public partial class 商品设置 : Form
    {
        public 商品设置()
        {
            InitializeComponent();
        }
        private void 商品设置_Load(object sender, EventArgs e)
        {
            //商品类别查询
            DataSet ds = 商品设置BLL.ProductType();
            this.dataGridView1.DataSource = ds.Tables[0];

            //comboBox1绑定
            DataSet da = 商品设置BLL.ProductType();
            DataRow row = da.Tables[0].NewRow();
            row[0] = 0;
            row[1] = "所有项目";
            da.Tables[0].Rows.InsertAt(row,0);
            this.comboBox1.DataSource = da.Tables[0];
            //隐藏值
            this.comboBox1.ValueMember = "PTID";
            //显示值
            this.comboBox1.DisplayMember = "PTName";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取隐藏值(赋值)
            Products.PTID = comboBox1.SelectedValue.ToString();
            //执行操作
            DataSet ds = 商品设置BLL.Product();
            this.dataGridView2.DataSource = ds.Tables[0];
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           //赋值
            Products.ProductJP = textBox1.Text;
           DataSet ds= 商品设置BLL.ProductsChaxun();
           this.dataGridView2.DataSource = ds.Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //输入框
            string str = Interaction.InputBox("请输入添加名称", "添加商品类别", "", -1, -1);
            //空格替换为空
            str = str.Replace(" ", "");
            if (str == "")
            {
                 return;
            }
            //赋值
            Products.PTName = str;
            object o = 商品设置BLL.ChaDan();
            if (Convert.ToInt32(o) <=0)
            {
                //执行操作
                int count = 商品设置BLL.ProductTypeAdd();
                if (count > 0)
                {
                    MessageBox.Show("添加成功");
                    商品设置_Load(null, null);
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                MessageBox.Show("改房间存在");
            }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //判断选中的数量
            if (dataGridView1.SelectedRows.Count > 1)
            {
                MessageBox.Show("请选择一行");
                return;
            }
            //获取选中项的值
            string PTID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            //赋值
            Products.PTID = PTID;
            //输出框
            string PTName = Interaction.InputBox("请输入修改名称", "修改商品类别", "", -1, -1);
            //空格替换为空
            PTName = PTName.Replace(" ", "");
            if (PTName == "")
            {
                return;
            }
            //赋值
            Products.PTName = PTName;
            //执行操作
            int count = 商品设置BLL.ProductTypeUpdate();
            if (count > 0)
            {
                MessageBox.Show("修改成功");
                商品设置_Load(null, null);
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            //判断选中的数量
            if (dataGridView1.SelectedRows.Count > 1)
            {
                MessageBox.Show("请选择一行");
                return;
            }
            //获取选中项的值
            string PTID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            //赋值
            Products.PTID = PTID;
            DataSet ds = 商品设置BLL.ProductTypeChuProducts();
            if (ds.Tables[0].Rows.Count > 0)
            {
                MessageBox.Show("请先删除商品");
            }
            else
            {
                DialogResult dr = MessageBox.Show("你确定要删除改商品类别吗？", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    int count = 商品设置BLL.ProductTypeDelete();
                    if (count > 0)
                    {
                        MessageBox.Show("删除成功");
                        商品设置_Load(null, null);
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //判断选中的数量
            if (dataGridView1.SelectedRows.Count > 1)
            {
                MessageBox.Show("请选择一行");
                return;
            }
            //获取选中的值
            string ProductID = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
            //传值
            Products.ProductID = ProductID;
            //执行操作
            int count = 商品设置BLL.ProductsDelete();
            if (count > 0)
            {
                MessageBox.Show("删除成功");
                商品设置_Load(null, null);
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            商品添加 f = new 商品添加();
            f.Show();
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //获取数据
            string ProductID = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
            string ProductName = dataGridView2.SelectedRows[0].Cells[1].Value.ToString();
            string ProductPrice = dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
            string ProductJP = dataGridView2.SelectedRows[0].Cells[4].Value.ToString();
            string PTName = dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
           //赋值
            Products.ProductID = ProductID;
            Products.ProductName = ProductName;
            Products.ProductPrice = ProductPrice;
            Products.ProductJP = ProductJP;
            Products.PTName = PTName;
            //跳转
            商品修改 f = new 商品修改();
            f.Show();
            this.Close();
        }
    }
}
